<?php

/*
Copyright 2007 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

class BH_Widget_Data {
	
	static private $default_classes = array(
		'BH_Archive_Calendar_Widget',
		'BH_Links_Widget',
		'BH_RSS_Widget',
		'BH_Recent_Entries_Widget',
		'BH_Spacer_Widget',
		'BH_Category_Dropdown_Widget',
		'BH_Pages_Widget',
		'BH_Recent_Comments_Widget',
		'BH_Search_Widget',
		'BH_Text_Widget',
	);

	private $classes;
	private $widgets;
	private $f = false;
		
	function __construct() {
	
		global $wpdb;
		
		array_push($wpdb->tables, 'widgets');
		$wpdb->set_prefix($wpdb->prefix);
		
		$this->classes = array();
		$this->widgets = array();
		
		foreach ($wpdb->get_results('select * from '. $wpdb->widgets .' where widget_delete=\'0\'') as $widget) {
			$widget->widget_attributes = unserialize($widget->widget_attributes);
			$this->widgets[$widget->widget_id] = $widget;
		}
	}
	
	function get_classes() {
		return $this->classes;
	}
	
	function add_class($class, $x = 460, $y = 350) {
		$this->set_class($class, array ( 'x' => $x, 'y' => $y));
	}
	
	function remove_class($class) {
		$this->set_class($class, null);
	}
	
	function get_class($class) {
		return $this->classes[$class];
	}
	
	function set_class($class, $ctrl_dim) {
		$this->classes[$class] = $ctrl_dim;
	}
	
	function get_widgets() {
		return $this->widgets;
	}
	
	function create_widget($name, $class, $attributes = array()) {
	
		$ids = array_keys($this->widgets);
		
		if (count($ids)) {
			$widget->widget_id = max($ids) + 1;
		} else {
			$widget->widget_id = 1;
		}
		
		$widget->widget_name = $name;
		$widget->widget_class = $class;
		$widget->widget_attributes = $attributes;
		$widget->widget_delete = 0;
		
		$this->widgets[$widget->widget_id] = $widget;
		$this->f = true;
		return $widget;
	}
		
		
	
	function get_widget($id) {
		return $this->widgets[$id];
	}
	
	function set_widget($id, $widget) {
		$this->widgets[$id] = $widget;
		$this->f = true;
	}
	
	function set_widget_name($id, $name) {
		$this->widgets[$id]->widget_name = $name;
		$this->f=true;
	}
	
	function set_widget_class($id, $class) {
		$this->widgets[$id]->widget_class = $class;
		$this->f=true;
	}
	function set_widget_attributes($id, $attributes) {
		$this->widgets[$id]->widget_attributes = $attributes;
		$this->f=true;
	}

	
	function set_widget_delete($id, $delete) {
		$this->widgets[$id]->widget_delete = $delete ? 1 : 0;
		$this->f=true;
	}
	
	function find_widget($name) {
		foreach ($this->widgets as $id => $widget) {
			if ($widget->widget_name == $name)
				return $id;
		}
		
		return false;
	}
	
	function shutdown() {
		global $wpdb;
		
		do_action('bh_widget_data_update');
		
		if (!$this->f) return;
		
		foreach ($this->widgets as $w) {
			$sql  = "replace into ". $wpdb->widgets ." (widget_id, widget_name, widget_class, widget_attributes,widget_delete) ";
			$sql .="values ('". $w->widget_id ."','".$w->widget_name ."','". $w->widget_class ."','". addslashes(serialize($w->widget_attributes)) ."','". $w->widget_delete ."')";
			$wpdb->query($sql);
		}
		//removes empty items
		$this->widgets = array_filter($this->widgets);
		//update_option('bh-widgets-objects', serialize($this->widgets));
	}
}

if (get_option('bh_widgets_db_version')) {
	global $bhwd;
	$bhwd = new BH_Widget_Data;
	add_action('shutdown', array($bhwd,'shutdown'));
}

?>
